package com.feo.kpler

import android.os.CountDownTimer
import android.util.Log
import android.widget.VideoView
import com.feo.kpler.videocache.CacheListener
import java.io.File

/**
 * 可以用来做无界面音频播放
 */
class FKerVideoPlayerManager(private val videoView: VideoView) : CacheListener {

    var listenerListener: FKerVideoProgressListener? = null

    private var mCountDownTimer: CountDownTimer? = null
    private var currentPositionPlay = 0
    private var isPlaying = false

    fun start(url: String) {
        isPlaying = true
        var mProxyURL = url
        HttpMediaCacheServer.getProxy()?.let {
            it.registerCacheListener(this, url)
            mProxyURL = it.getProxyUrl(url)
        }
        videoView.setVideoPath(mProxyURL)
        videoView.start()



        videoView.setOnCompletionListener {
            mCountDownTimer?.cancel()
            listenerListener?.complete()
        }

        videoView.setOnPreparedListener {
            Log.i("chyy","setOnPreparedListener")
            listenerListener?.start()
            initCountDownTime()
        }

    }

    //主动调用暂停播放
    fun activePause() {
        if (!isPlaying) {
            return
        }
        isPlaying = false
        videoView.pause()
        mCountDownTimer?.cancel()
    }

    // 主动调用继续播放
    fun activeResume() {
        if (isPlaying) {
            return
        }
        isPlaying = true
        videoView.seekTo(currentPositionPlay)
        videoView.start()
        mCountDownTimer?.start()
    }

    // 切换app，声明周期变化，自动调用暂停播放
    fun pause() {
        if (isPlaying) {
            videoView.pause()
            mCountDownTimer?.cancel()
        }
    }

    // 切换app，声明周期变化，自动调用继续播放
    fun resume() {
        if (isPlaying) {
            videoView.seekTo(currentPositionPlay)
            videoView.start()
            mCountDownTimer?.start()
        }
    }

    fun destroy() {
        listenerListener = null
        mCountDownTimer?.cancel()
        videoView.stopPlayback()
    }

    //缓存进度
    override fun onCacheAvailable(cacheFile: File?, url: String?, percentsAvailable: Int) {
    }

    //注册进度监听
    fun registerProgressListener(listenerListener: FKerVideoProgressListener) {
        this.listenerListener = listenerListener
    }

    private fun initCountDownTime() {
        Log.i("chyy", "initCountDownTime")
        mCountDownTimer?.cancel()
        mCountDownTimer = null

        mCountDownTimer = object : CountDownTimer(Long.MAX_VALUE, 1000) {
            override fun onTick(p0: Long) {
                //没有获取到音频时长
                if (videoView.duration == -1) {
                    return
                }
                currentPositionPlay = videoView.currentPosition
                listenerListener?.onProgress(videoView.currentPosition, videoView.duration)
                //播放完成，停止播放
                if (videoView.currentPosition == videoView.duration) {
                    isPlaying = false
                    cancel()
                }
            }

            override fun onFinish() {
            }

        }

        mCountDownTimer?.start()
    }


}
